using System;
using System.Collections.Generic;
using System.Linq;
using BuildWheel;
using UnityEngine;
using UnityEngine.UIElements;

public class ProjectTree : MonoBehaviour
{
    private UIDocument uiroot;
    private TreeView treeView;
    private Label stats;
    public FileInfo select;
    private Dictionary<int, FileInfo> _dictionary = new Dictionary<int, FileInfo>();

    void Start()
    {
        uiroot = GetComponent<UIDocument>();
        treeView = uiroot.rootVisualElement.Q<TreeView>("treeGroup");
        stats = uiroot.rootVisualElement.Q<Label>("stats");
    }


    void RefChild(ref List<TreeViewItemData<Folders>> parentList, Folders folder, ref int index)
    {
        int folderCount = folder.childFolders.Count;
        int fileCcount = folder.files.Count;
        int total = folderCount + fileCcount;
        if (total == 0) return;


        var itemlist = new List<TreeViewItemData<Folders>>(total);
        index += 1;
        var floderItem = new TreeViewItemData<Folders>(index, folder, itemlist);
        parentList.Add(floderItem);
        _dictionary.TryAdd(index, folder);
        for (var i = 0; i < folderCount; i++)
        {
            var child = folder.childFolders[i];
            RefChild(ref itemlist, child, ref index);
        }

        // for (int j = 0; j < fileCcount; j++)
        // {
        //     var child = folder.files[j];
        //     index += 1;
        //     var item = new TreeViewItemData<string>(index, child.name);
        //     itemlist.Add(item);
        //     _dictionary.TryAdd(index, child);
        // }
    }

    public void Ref(Folders root)
    {
   
        var items = new List<TreeViewItemData<Folders>>(root.childFolders.Count);

        int index = -1;
        RefChild(ref items, root, ref index);
        Func<VisualElement> makeItem = () => new Label();

        Action<VisualElement, int> bindItem = (e, i) =>
        {
            var item = treeView.GetItemDataForIndex<Folders>(i);
            (e as Label).text = item.n;
        };
        treeView.SetRootItems(items);
        treeView.makeItem = makeItem;
        treeView.bindItem = bindItem;
        treeView.selectionType = SelectionType.Multiple;
        treeView.Rebuild();

// Callback invoked when the user double clicks an item
        treeView.itemsChosen += (selectedItems) => { Debug.Log("Items chosen: " + string.Join(", ", selectedItems)); };

// Callback invoked when the user changes the selection inside the TreeView
        treeView.selectedIndicesChanged += (selectedItems) =>
        {
            select = treeView.GetItemDataForIndex<Folders>(treeView.selectedIndex);
            FolderDetailTree.instance.Ref(select as Folders);
        };

        FolderDetailTree.instance.Ref(root);
        treeView.ExpandItem(0);
        //https://discussions.unity.com/t/how-to-turn-off-drag-and-drop-in-treeview/1633728/4
        //    treeView.reorderable = false; // unity 6.047 修复禁用拖拽功能
    }

    private void Update()
    {
        stats.text = Stats.statsStr;
    }
}